<template>
  <div class="dep_main">
    <!-- <h1>权限管理</h1> -->
    <div class="btn_div">
      <el-button type="success" style="background-color: #42B983;" @click="dialogFormVisible = true">新增权限</el-button>
    </div>

    <!-- 自定义的表格组件 -->
    <Table ref="mytable" :param="table"></Table>


    <!-- 新增权限的弹窗 -->
    <el-dialog title="新增权限" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="权限名称" :label-width="formLabelWidth">
          <el-input v-model="form.powerName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="父选择" :label-width="formLabelWidth">
          <!-- 下拉框 -->
          <el-select v-model="form.parentNode" placeholder="请选择" @change="selectParentPower()">
            <el-option
              v-for="p in powers"
              :key="p.id"
              :label="'(' + p.type + ')' + p.powerName"
              :value="p.id + '-' + p.type"
              :disabled="p.type == 3">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="权限标识" :label-width="formLabelWidth">
          <el-input v-model="form.powerTag" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="资源路径" :label-width="formLabelWidth">
          <el-input v-model="form.powerResource" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="insertPower()">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import Table from './Table'

  export default{
    components:{
      Table
    },
    data(){
      return {
        formLabelWidth: '120px',
        //可选的权限列表
        powers: [],
        //表单数据
        form: {
          parentNode: "",
          powerName: "",
          pid: "",
          powerTag: "",
          powerResource: "",
          type: 1
        },
        //弹出框的显示标识
        dialogFormVisible: false,
        //表格组件的属性
        table: {
          title: [
            {name: "编号", col:"id"},
            {name: "父权限名称", col:"parentName", default:"空"},
            {name: "权限名称", col:"powerName"},
            {name: "权限标识", col:"powerTag", default:"空"},
            {name: "资源路径", col:"powerResource", default:"空"},
            {name: "权限类型", col:"type"}
          ],
          url: this.$.url.powerlist,
          page: {
            enable: true, //是否开启表格组件的分页
            pageSize: 5 //每页显示多少条
          },
          btns: [
            {
              btnName: "编辑",
              click: function(rowData){
                console.log("编辑：" + JSON.stringify(rowData));
              }
            },
            {
              btnName: "删除",
              click: function(rowData){
                console.log("删除：" + JSON.stringify(rowData));
              }
            }
          ]
        }
      }
    },
    methods:{
      //选择父级权限时触发
      selectParentPower(){
        var data = this.form.parentNode;
        var datas = data.split("-");
        var pid = datas[0];
        var type = datas[1];

        this.form.pid = pid;
        this.form.type = parseInt(type) + 1;
      },
      //查询权限列表 - 新增时
      queryPowerByInster(){
        this.$.ajax({
          type: "GET",
          url: this.$.url.powerlistByInsert,
          success: (data) => {
            if(data.code == 200){
              //成功
              this.powers = data.data;
            }
          }
        });
      },
      //新增权限
      insertPower(){
        //1、先发ajax
        this.$.ajax({
          type: "POST",
          url: this.$.url.powerinsert,
          data: this.form,
          success: (data) => {
            if(data.code == 200){
               //新增成功
               //2、关闭弹窗
               this.dialogFormVisible = false;
               //3、刷新表格
               this.$refs.mytable.request();
               //4、刷新下拉框
               this.queryPowerByInster();
            } else if(data.code == 405){
               //参数校验异常
               var error = data.data;
               //获取当前对象中的所有value
               for(var er in error){
                 var info = error[er]
                 this.$message.error(info);
               }
            }
          }
        });
      }
    },
    mounted(){
      //加载可选的父权限列表
      this.queryPowerByInster();
    }
  }
</script>

<style>
  .dep_main {
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: center;
    height: 100%;
    width: 100%;
  }

  .table_cls {
    margin-top: 10px;
  }

  .btn_div {
    /* margin-top: 50px; */
    background-color: #E9EEF3;
    text-align: left;
    width: 100%;
    height: 60px;
    display: flex;
    flex-direction: row;
    justify-content: left;
    align-items: center;
    padding-left: 50px;
  }
  
  .btn_div > * {
    margin-right: 10px;
  }
</style>
